package com.dhf.framework.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.web.entity.Housecard;
import com.dhf.framework.web.entity.Person;
import com.dhf.framework.web.service.HousecardService;
import com.dhf.framework.web.service.PersonService;



 /**
 * </b>HousecardController<br>项目
 * @author HoraceHo
 * @version:1.0
 */
@Controller
@RequestMapping("/web/housecard")
public class HousecardController extends BaseController{
	private static Logger LOGGER = LoggerFactory.getLogger(HousecardController.class);

	@Autowired
	private HousecardService housecardService;
	
	@Autowired
	private PersonService personService;
	
	/**
	  * 
	  * @Title: queryPageList
	  * @Description: 查询房产证列表页面
	  * @param @param searchEntity
	  * @param @param pagination
	  * @return ModelAndView    返回类型
	  * @throws
	*/
	@RequestMapping(value = "/queryPageList")
	public ModelAndView queryPageList(Housecard housecard, Pagination<Housecard> pagination) {
		LOGGER.debug(HousecardController.class.getName()+":开始查询分页数据！");
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView( "web/housecard/housecardList" );
		//调用housecardService中的方法，查询分页数据
		pagination = this.housecardService.queryPageList( housecard, pagination );
		mv.addObject( "pagination", pagination );
		return mv;
	}
	
	
	/**
	 * 
	 * @Title: toMerge
	 * @Description: 新增或修改页面
	 * @param @param entity
	 * @return ModelAndView    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(Housecard entity){
		//创建ModelAndView对象，用来存放数据和视图。
		ModelAndView mv = new ModelAndView("web/housecard/housecardMerge");
		if(entity.getId()!= null){
			Housecard housecard =  this.housecardService.get( entity.getId() ) ;
			List<Person> personList = this.personService.listAll();
			mv.addObject("housecard", housecard);
			mv.addObject("personList", personList);
		}else{
			List<Person> personList = this.personService.listAll();
			
			mv.addObject("housecard", new Housecard());
			mv.addObject("personList", personList);
		}
		return mv;
	}

	/**
	 * 
	* @Title: doMerge
	* @Description: 新增或修改数据 
	* @param @param request
	* @param @param entity
	* @return ModelAndView    返回类型
	* @throws
	 */
	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(HttpServletRequest request, Housecard entity,RedirectAttributes redirectAttributes) {
		//判断应该新增数据还是修改数据。
		if (entity.getId()!=null) {
			LOGGER.debug(HousecardController.class.getName()+":开始更新数据！");
			this.addMessage(redirectAttributes, "修改数据成功");
			entity.setModifyTime(new Date());
			//调用service层的方法，修改entity对象的信息。
			this.housecardService.update( entity );
		} else {
			LOGGER.debug(HousecardController.class.getName()+":开始新增数据！");
			entity.setCreateTime(new Date());
			//调用service层的方法，新增entity对象的信息。
			this.housecardService.add( entity );
			this.addMessage(redirectAttributes, "新增据成功");
		}
		return new ModelAndView( "redirect:/web/housecard/queryPageList.do" );
	}

	/**
	 * 
	 * @Title: delete
	 * @Description:删除数据
	 * @param @param id
	 * @return ModelAndView    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/delete")
	public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
		LOGGER.debug(HousecardController.class.getName()+":开始删除数据！");
		this.housecardService.delete( id );
		this.addMessage(redirectAttributes, "删除数据成功");
        return new ModelAndView( "redirect:/web/housecard/queryPageList.do" );
		
        
        
	}
}

